<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>制造计划</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui/css/layui.css" media="all">

    <script type="text/javascript" src="../../../res/js/jquery-3.4.1.min.js"></script>
    <script src="../../../res/js/util.js"></script>
</head>

<body>

<input type="hidden" id="planId">
<table class="layui-hide" class="list" id="test" lay-filter="test"></table>


<table class="layui-hide" id="test1" lay-filter="test1"></table>
<div style="padding:  2% 45%;">
    <button class="layui-btn" id="baocun" lay-event="getData">保存</button>
</div>

<script src="../../../res/layui/layui.js" charset="utf-8"></script>
<script>


    var el = null;
    var openIndex = null;
    var table = null;
    var array = new Array();
    var s = 0;
    layui.use(['table', 'layer'], function () {
        table = layui.table;
        refresh(array)
        var planId = getQueryString("id");

        table.render({
            elem: '#test'
            , url: '../../../manufacturePlan/selectOne?planId='+planId
            //, toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            /*, defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                title: '提示'
                , layEvent: 'LAYTABLE_TIPS'
                , icon: 'layui-icon-tips'
            }]*/
            , title: '制造计划'
            , cols: [[
                {type: 'checkbox',LAY_CHECKED:false,fixed: 'left'}
                , {field: 'drugName', title: '药品名称', width: 120}
                , {field: 'mpTask', title: '计划完成任务', width: 100}
                , {field: 'mpFinished', title: '是否完成', width: 150}
                , {field: 'mpUnfinished', title: '未完成部分', width: 150}
                , {field: 'mpScheme', title: '处理方案', width: 100}
                // , {fixed: 'right', title: '操作', toolbar: '#barDemo', width: 220}
            ]]
            , page: true
            , parseData: function (res) {//res 即为原始返回的数据
                ids = res.data;
                for(var i in res.data){
                    for(var j=0;j<array.length;j++){
                        if(array[j].mpId==res.data[i].mpId){

                            //如果set集合中有的话，给rows添加check属性选中
                            res.data[i]["LAY_CHECKED"] = true;
                        }
                    }

                }

                i = 0;
                //遍历表格所有行  改行状态为0 则选择框不可选
                $(".layui-table-fixed.layui-table-fixed-l").find(".layui-table-body").find("input[name='layTableCheckbox']").each(function() {
                    if (res.data[i].mpUnfinished == 0) {
                        $(this).attr("disabled", 'disabled').removeAttr("checked");
                        form.render('checkbox');
                    }
                    i++;
                });



                return {
                    "code": res.code, //解析接口状态
                    "count": res.count, //解析数据长度
                    "data": res.data //解析数据列表
                };
            }
        });


        //头工具栏事件
        table.on('toolbar(test1)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'getData':

                    var data = array;
                    if(data==null){
                        layer.msg('请选择制造的药品', {icon: 5, time: 2000});
                    }else{
                        for(let i = 0;i<data.length;i++){
                            data[i].mpUnfinished -= data[i].num;
                        }
                        var url = "../../../manufacturePlan/madeDrug";
                        $.post(url, {jsonStr:JSON.stringify(data)},function (obj) {
                            layer.msg(obj.message)
                            if(obj.status==20){
                                parent.closeOpen(obj.message);
                            }
                        },'json')
                        console.info(data)
                    }

                    break;

            };
        });


        table.on('checkbox(test)', function(obj){
            var checkStatus = table.checkStatus('test');
            var data = checkStatus.data;

            if(obj.type=="all"){

                if(data.length==0){
                    console.info(ids);
                    for(var i = 0;i<ids.length;i++){j
                        for(var j=0;j<array.length;j++){
                            if(ids[i].mpId==array[j].mpId ){
                                array.splice(j,1)
                            }
                        }

                    }
                }else{
                    $.each(data,function (index,item) {
                        if(JSON.stringify(array).indexOf(JSON.stringify(item.mpId))<0){
                            item.num=1;
                            array.push(item)
                        }

                    })

                }
            }else{
                if(!obj.checked){
                    for(var i=0;i<array.length;i++){
                        if(JSON.stringify(obj.data.mpId)==JSON.stringify(array[i].mpId)){
                            array.splice(i,1);
                            break;
                        }
                    }

                }else{
                    $.each(data,function (index,item) {
                        if(JSON.stringify(array).indexOf(JSON.stringify(item.mpId))<0){
                            item.num=1;
                            array.push(item)
                        }

                    })
                }
            }




            refresh(array);
        });

        table.on('edit(test1)', function(obj){ //注：edit是固定事件名，test是table原始容器的属性 lay-filter="对应的值"
            console.info(obj.data)

            var dd = obj.value.replace(/[^\d]/g,"");  //清除“数字”和“.”以外的字符
            if(dd == ""){
                dd =1;
            }else if(dd.indexOf("0")==0){
                dd = dd.substring(1,dd.length);
            }else if(dd > obj.data.mpUnfinished){
                dd = 1
                layer.msg('制造数量不能大于未制造药品数量', {icon: 5, time: 2000});
            }

            $(this).val(dd);

            for(var i=0;i<array.length;i++){
                if(array[i].drugId==obj.data.drugId){
                    array[i].num = dd
                }
            }

            /*refresh(array);*/

        });
    });


    $("#baocun").on('click',function () {
        var data = array;
        if(data.length<1){
            layer.msg('请选择要制造的药品', {icon: 5, time: 2000});
        }else{
            s+=1;
            if(s==1){
                for(let i = 0;i<data.length;i++){
                    data[i].mpUnfinished -= data[i].num;
                }
                var url = "../../../manufacturePlan/madeDrug";
                $.post(url, {jsonStr:JSON.stringify(data)},function (obj) {
                    layer.msg(obj.message)
                    if(obj.status==20){
                        parent.closeOpen(obj.message);
                    }else{
                        s=0;
                    }
                },'json')
            }
        }
    })


    /**
     * 关闭层
     */
    function closeOpen(msg) {
        layer.close(openIndex);
        layer.msg(msg, {time: 1000}, function () {
            table.reload("test");//刷新表格
        })
    }

    function refresh(obj){
        table.render({
            elem: '#test1'
            , data: obj
            //,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            , defaultToolbar:false
            , title: '用户数据表'
            //,limit:obj.length
            //, toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            , cols: [[
                {field: 'drugName', title: '药品名称', width: '20%'}
                , {field: 'mpTask', title: '计划完成任务', width: 100}
                , {field: 'mpUnfinished', title: '未完成部分', width: 150}
                , {field: 'num', title: '本次制造数量', width: '20%',edit:'test'}
            ]]
            , page: true
            ,text: {none: '请从上表选择药品开始制造'}
        });
    }


</script>
</body>
</html>